package com.lida.zwservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lida.zwservice.entity.*;
import com.lida.zwservice.mapper.PersonalDetailedMapper;
import com.lida.zwservice.service.PersonalDetailedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lida.zwservice.service.PersonalDetailedSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 账务表 服务实现类
 * </p>
 *
 * @author LiDa
 * @since 2022-01-20
 */
@Service
public class PersonalDetailedServiceImpl extends ServiceImpl<PersonalDetailedMapper, PersonalDetailed> implements PersonalDetailedService {

    @Resource
    private PersonalDetailedMapper personalDetailedMapper;

    @Autowired
    private PersonalDetailedSubjectService subject;

    @Override
    public GetDetailedByIdVo getDetailedById(String id) {
        GetDetailedByIdVo detailed =personalDetailedMapper.getDetailedById(id);
        return detailed;
    }

    @Override
    public List<PersonalDetailed> selectItems(ExcelDetailedVo detailedVo) {
        String title=detailedVo.getTitle();
        String max=detailedVo.getMax();
        String min=detailedVo.getMin();
        String type=detailedVo.getType();

        List time=detailedVo.getTime();
        String begin="";
        String end="";
        if(time.size()!=0){
            begin=time.get(0).toString();
            end =time.get(1).toString();
        }

        QueryWrapper<PersonalDetailed> wrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(max)){
            wrapper.le("num",Integer.valueOf(max));
        }
        if(!StringUtils.isEmpty(min)){
            wrapper.ge("num",Integer.valueOf(min));
        }
        if(!StringUtils.isEmpty(type)){
            wrapper.eq("type",type);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        List<PersonalDetailed> items=baseMapper.selectList(wrapper);

        List<PersonalDetailed> finalList = new ArrayList<PersonalDetailed>();
        for(int i=0;i<items.size();i++){
            PersonalDetailed detailed =items.get(i);
            String typeId = detailed.getType();
            String sortId =detailed.getSortId();
//            QueryWrapper<PersonalDetailedSubject> wrapperSubject=new QueryWrapper<PersonalDetailedSubject>();
//            wrapperSubject.eq("id",typeId);
            PersonalDetailedSubject byId = subject.getById(typeId);
            PersonalDetailedSubject bySortId=subject.getById(sortId);
//            PersonalDetailed finalDetailed=new PersonalDetailed();
//            BeanUtils.copyProperties(detailed,finalDetailed);
            detailed.setType(byId.getTitle());
            detailed.setSortId(bySortId.getTitle());


            finalList.add(detailed);
        }
        return finalList;
    }
}
